﻿<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeFile="Promotions.aspx.cs" Inherits="Promotion_Promotions" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
    <script src="../JavaScript/My97DatePicker/WdatePicker.js" type="text/javascript"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <script src="../JavaScript/common.js" type="text/javascript"></script>
    <script src="../JavaScript/validatebox.js" type="text/javascript"></script>
    <script src="../JavaScript/PageGrid.js" type="text/javascript"></script>
    <script type="text/javascript">
        var p_data = eval('(' + '<%= ddl_Promotion %>' + ')');
        var u_data = eval('({"Table1":[{"ID":"PC","NAME":"盒"},{"ID":"CS","NAME":"箱"}]})');
        var columns = [[{ field: "c", title: "活动代码", width: 100 },
        { field: "n", title: "活动名称", width: 200 },
        { field: "s", title: "开始时间", width: 80 },
        { field: "e", title: "结束时间", width: 80 },
        { field: "st", title: "状态", width: 80, formatter: function (value, rec) {
            return (value == '0') ? '<span>未开始</span>' : ((value == '1') ? '<span style="color:green;">正在进行</span>' : '<span>已结束</span>');
        }
        },
        { field: "de", title: "描述", width: 300 }
        ]];

        function AjaxPost(method, obj, arr) {
            var dataArr = "{'d': '<%= MyStateManager.DomainID %>','l':'<%= MyStateManager.UserID %>'" + arr + "}";
            $.ajax({
                url: "Promotions.aspx/" + method,
                data: dataArr,
                type: "post",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (getdata) {
                    switch (obj) {
                        case "bindGrid":
                            bindGrid(getdata);
                            break;
                        case "Save":
                            Saveed(getdata);
                            break;
                    }
                },
                error: function (xhr) {
                    $.messager.alert('警告消息', xhr.responseText, 'warning');
                }
            });
        }
        function load() {
            $('#tb').datagrid({
                minWidth: 500,
                height: 450,
                rownumbers: true,
                columns: columns,
                singleSelect: true,
                toolbar: [{
                    text: '新增',
                    iconCls: 'icon-add',
                    handler: function () { reset(); open1(); }
                }, '-', {
                    text: '修改',
                    iconCls: 'icon-edit',
                    handler: function () {
                        if ($('#tb').datagrid('getSelected') != null) { editGroup($('#tb').datagrid('getSelected')); open1(); }
                        else { $.messager.alert('警告消息', '请选择一行数据.', 'warning'); }
                    }
                }, '-', {
                    text: '删除',
                    iconCls: 'icon-remove',
                    handler: function () {
                        if ($('#tb').datagrid('getSelected') != null) {
                            $.messager.defaults.ok = '确认';
                            $.messager.defaults.cancel = '取消';
                            $.messager.confirm('提示消息', '确认删除数据?', function (b) { if (b) { Save(2); } });
                        }
                        else { $.messager.alert('警告消息', '请选择一行数据.', 'warning'); }
                    }
                }, '-', {
                    text: '编辑产品',
                    iconCls: 'icon-redo',
                    handler: function () {
                        if ($('#tb').datagrid('getSelected') != null) {
                            location.href = "PromotionsProduct.aspx?id=" + $('#tb').datagrid('getSelected').ID;
                        }
                        else { $.messager.alert('警告消息', '请选择一行数据.', 'warning'); }
                    }
                }],
                onSelect: function (i, r) {
                    $('#hidden1').val(r.ID);
                }
            });
            $('#divS').window({
                title: '促销活动维护',
                minimizable: false,
                maximizable: false,
                collapsible: false,
                resizable: false,
                modal: true,
                top: ($(window).height() - 400) / 2
            });
            resetCombo(); close1();
        }
        function bindGrid(getdata) {
            if (getdata.d != "") {
                var data = eval('(' + getdata.d + ')');
                $('#tb').datagrid('loadData', data);
            }
        }
        function Saveed(getdata) {
            if (getdata.d == "1") {
                $.messager.alert('提示消息', '提交数据成功!', 'info');
                close1();
                AjaxPost('BindData', 'bindGrid', '');
            }
            else if (getdata.d == "") {
                $.messager.alert('提示消息', '活动代码存在!', 'info');
            }
        }
        function Save(i) {
            var arr = "";
            switch (i) {
                case 0:
                    arr = ",'i':-1,'c':'" + $('#text1').val().filter() + "','n':'" + $('#text2').val() + "','s':'" + $('#start_date').val() + "','e':'" + $('#end_date').val() + "','de':'" + $('#de').val() + "'";
                    break;
                case 1:
                    arr = ",'i':" + $('#hidden1').val() + ",'c':'" + $('#text1').val() + "','n':'" + $('#text2').val() + "','s':'" + $('#start_date').val() + "','e':'" + $('#end_date').val() + "','de':'" + $('#de').val() + "'";
                    break;
                case 2:
                    arr = ",'i':" + $('#tb').datagrid('getSelected').ID + ",'c':'','n':'','s':'-1','e':'-1','de':''";
                    break;
            }
            AjaxPost('Svae', 'Save', arr);
        }
        function editGroup(data) {
            $('#text1').val(data.c);
            $('#text2').val(data.n);
            $('#start_date').val(data.s);
            $('#end_date').val(data.e);
            $('#de').val(data.de);
            $('#text3').val(data.sv);
            $('#text4').val(data.pv);
            $('#select1').combobox('setValue', data.su);
            $('#select2').combobox('setValue', data.pu);
            $('#select3').combobox('setValue', data.ht);
            $('#hidden1').val(data.ID);
        }
        function reset() {
            $('#text1').val("");
            $('#text2').val("");
            $('#start_date').val("");
            $('#end_date').val("");
            $('#de').val("");
            $('#text3').val("");
            $('#text4').val("");
            $('#hidden1').val("");
            resetCombo();
        }
        function resetCombo() {
            $('#select1').combobox('setValue', 'PC');
            $('#select2').combobox('setValue', 'PC');
            $('#select3').combobox({
                width: 130, editable: false, valueField: 'ID', textField: 'NAME', value: '1'
            });
            $('#select3').combobox('loadData', p_data.Table1);
        }
        function open1() {
            $('#divS').window('open');
            $('#divS').css({ display: 'block' });
        }
        function close1() {
            $('#divS').window('close');
        }
        $(function () {
            load();
            AjaxPost('BindData', 'bindGrid', '');
            $('#btn_Save').click(function () {
                var b = true;
                $('#tab input').each(function () {
                    if ($(this).attr('required') || $(this).attr('validType'))
                        if (!$(this).validatebox('isValid')) { b = false; return false; }
                });
                if (b)
                    if ($('#hidden1').val() == "") Save(0); else Save(1);
            });
        });
    </script>
    <table id="wsd_inputtable">
        <tr>
            <td class="tabletitle" style="height: 21px" width="100%">
                促销活动列表
            </td>
        </tr>
    </table>
    <table id="wsd_listtable">
        <tr>
            <td style="width: 40%">
                <table id="tb">
                </table>
                <div id="page">
                </div>
            </td>
        </tr>
    </table>
    <div id="divS" class="easyui-window" icon="icon-save" style="width: 600px; height: 280px;
        padding: 5px; background: #fafafa; display: none;">
        <div class="easyui-layout" fit="true">
            <div region="center" border="false" style="padding: 10px; background: #fff; border: 1px solid #ccc;">
                <table style="margin: 0 0 0 0; border: 0; width: 100%; height: 180px;" id="tab">
                    <tr>
                        <td>
                            <label style="width: 80px; padding-left: 10px;">
                                活动代码:</label>
                            <input label="活动代码:" class="easyui-validatebox" type="text" id="text1" required="true"
                                missingmessage="此项为必填项!" /><span style="color: Red; padding-left: 5px;">*</span>
                        </td>
                        <td>
                            <label style="width: 80px; padding-left: 10px;">
                                活动名称:</label>
                            <input class="easyui-validatebox" type="text" id="text2" required="true" missingmessage="此项为必填项!" /><span
                                style="color: Red; padding-left: 5px;">*</span>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <label style="width: 80px; padding-left: 10px;">
                                开始时间:</label>
                            <input type="text" class="Wdate" onfocus="WdatePicker({minDate:'#F{\'%y-%M-{%d}\'}'})"
                                id="start_date" />
                        </td>
                        <td>
                            <label style="width: 80px; padding-left: 10px;">
                                结束时间:</label>
                            <input type="text" class="Wdate" onfocus="WdatePicker({minDate:'#F{$dp.$D(\'start_date\')}',maxDate:'#F{$dp.$D(\'start_date\',{d:60});}'})"
                                id="end_date" />
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <label style="width: 80px; padding-left: 10px;">
                                活动描述:</label>
                            <textarea id="de" rows="3" cols="55"></textarea>
                        </td>
                    </tr>
                </table>
            </div>
            <div region="south" border="false" style="text-align: right; height: 30px; line-height: 30px;
                padding-top: 5px;">
                <input type="hidden" id="hidden1" />
                <a class="easyui-linkbutton" icon="icon-ok" href="javascript:void(0)" id="btn_Save">
                    提交</a> <a class="easyui-linkbutton" icon="icon-cancel" href="javascript:void(0)"
                        onclick="close1()">取消</a>
            </div>
        </div>
    </div>
</asp:Content>
